<template>
  <CommonPage :show-header="false">
    <article class="margin-top-sm flex  flex-column flex-sub">
      <section style="display: flex;justify-content: space-between;margin-bottom: 10px">
        <div style="display: flex">
          <SortSelect3 v-model:value="sortKey" @on-select="search"></SortSelect3>
        </div>
        <div style="display: flex">
          <n-button size="large" round type="success" style="font-size: 22px;margin: 0 10px" @click="handleCreate">创建
          </n-button>
          <SearchInput v-model:value="searchKey" @change="search" placeholder="ID/工号/医护姓名" :loading="loading"></SearchInput>
        </div>
      </section>
      <section  style="overflow:hidden" class="flex flex-sub radius-lg bg-white  padding-tb-sm">
            <CurdTable ref="tableRef"
                       :get-data="api.schedule.list"
                       :columns="columns"
                       :and-params="andParams"
                       :or-params="orParams"
                       :order-params="orderParams">
            </CurdTable>
      </section>
    </article>
  </CommonPage>
</template>

<script setup lang="ts">
import {h, onMounted, ref} from "vue";
import api from "@/api";
import CommonPage from "@/components/page/CommonPage.vue";
import CurdTable from "@/components/common/table/CurdTable.vue";
import SearchInput from "@/components/common/input/SearchInput.vue";
import NoticeModal from "@/components/example/modal/NoticeModal.vue";
import {NAvatar, NButton, NEllipsis, NText} from "naive-ui";
import myMoment from "@/utils/common/myMoment";
import SortSelect3 from "@/components/example/select/SortSelect3.vue";
import router from "@/router";
import {imagePathPrefix} from '~/settings'
/** QueryBar筛选参数（可选） */
const orParams = ref({
  id:'',
  nurseName:'',
  jobNum:'',
})
/** 补充参数（可选） */
const andParams = ref({})
/** 排序参数(可选）*/
const orderParams = ref({
  createTime:''
})
const sortKey =ref('2')
const searchKey = ref('')
const tableRef = ref()
const loading = ref()


const prefix =imagePathPrefix
const columns = ref([
  {title: 'ID', key: 'id', width: 3, ellipsis: {tooltip: true}},
  {
    title: '工号', key: 'jobNum', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.jobNum})
    }
  },
  {
    title: '头像', key: 'avatar', width:3, ellipsis: {tooltip: true},align: 'center',
    render(row: any) {
      return h(NAvatar, {
        width: 100,
        src: prefix+row.avatar || "https://www.zzjtpp.top/blogAPI/uploadFile/%E5%A4%B4%E5%83%8F.bmp",
        round: true
      })
    }
  },
  {
    title: '医生', key: 'nurseName', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.nurseName})
    }
  },
  {
    title: '起始时间', key: 'nurseName', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => myMoment(row.start).format("M-D HH:mm")})
    }
  },
  {
    title: '结束时间', key: 'nurseName', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => myMoment(row.end).format("M-D HH:mm")})
    }
  },
  {
    title: '预约数', key: 'appointmentNum', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.appointmentNum})
    }
  },
  {
    title: '创建时间', key: 'createTime', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: { [x: string]: any; }) {
      return h("div", {}, [
        h("div", {style: {fontSize: '14px'}}, [h(NEllipsis, {}, {default: () => myMoment(row?.createTime).format("MMMDo HH:mm")})]),
        h(NText, {depth: '3'}, {default: () => myMoment(row?.createTime).startOf('minute').fromNow()}),
      ])
    }
  },
  {
    title: '更新时间', key: 'updateTime', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: { [x: string]: any; }) {
      return h("div", {}, [
        h("div", {style: {fontSize: '14px'}}, [h(NEllipsis, {}, {default: () => myMoment(row?.updateTime).format("MMMDo HH:mm")})]),
        h(NText, {depth: '3'}, {default: () => myMoment(row?.updateTime).startOf('minute').fromNow()}),
      ])
    }
  },

  {
    title: '操作', key: 'actions', width: 8, align: 'center',
    render(row: any) {
      return [
        h(NButton, {
          round: true,
          size:'small',
          secondary: true,
          onClick: () => handleQuery(row)
        }, {default: () => "查看"}),
        h(NButton, {
          round: true,
          size:'small',
          secondary: true,
          type: "error",
          onClick: () => handleRemove(row)
        }, {default: () => "删除"}),]
    }
  }
])
onMounted(() => {
  search()
})

const handleCreate = () => {
  router.push({
    path:'/schedule/nurse'
  })
}
const handleQuery = (row: any) => {

}


const handleRemove = (row:any) => {
    window.$dialog.info({
        title: '是否确定删除？',
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick: () => {
            api.schedule.remove({
                id: row.id
            }).then(res => {
                if (res && res.data.code === 0) {
                    window.$message.success(res.data.mgs)

                }
            })
        },
        onNegativeClick: () => {

        },

    })
}

const search = async () => {
  loading.value = true
  Object.keys(orParams.value).forEach((key: any) => {
    //@ts-ignore
      orParams.value[key] = `%${searchKey.value}%`
  })
  if (sortKey.value === '1') {
    orderParams.value.createTime='1'
  }else if(sortKey.value==='2'){
    orderParams.value.createTime='2'
  }else {
    orderParams.value.createTime=''
  }
  await tableRef.value.handleSearch()
  loading.value = false
}

</script>

<style scoped>

</style>